﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.W.realize;

namespace MyMES.Writer.API.Application.CommendHandlers.W.realize
{
    public class UpdateSalesReceiptCommandHanders : IRequestHandler<UpdateSalesReceiptCommands, APIResult<SalesReceipt>>
    {
        private readonly IBaseRepository<SalesReceipt> baseRepository;
        private readonly IMapper mapper;

        public UpdateSalesReceiptCommandHanders(IBaseRepository<SalesReceipt> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }
        
        /// <summary>
        /// 修改销售出库信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<SalesReceipt>> Handle(UpdateSalesReceiptCommands request, CancellationToken cancellationToken)
        {
            APIResult<SalesReceipt> result = new APIResult<SalesReceipt>();
            result.Code=APIEnum.修改成功;
            result.Msg = "修改成功";
            var map = mapper.Map<SalesReceipt>(request);
            var list = $"Update SalesReceipt set OutboundOrderCode='{request.OutboundOrderCode}',OutboundOrderName='{request.OutboundOrderName}',SalesOrderNumber='{request.SalesOrderNumber}',CustomerCode='{request.CustomerCode}',OutboundDate=@OutboundDate,DocumentStatus='{request.DocumentStatus}',Remarks='{request.Remarks}',CustomerName='{request.CustomerName}',IsDelete=@IsDelete where SalesReceiptId = '{request.SalesReceiptId}'";
            await baseRepository.Update(map, list);
            return result;
        }
    }
}
